/*********************************************************************
 *
 *                 1-Wire Communication Protocol
 *
 *********************************************************************
 ********************************************************************/

 /****** I N C L U D E S **********************************************************/
#include "Config.h"
#include  "delay.h"
#include "1wire.h"
#include "OSA.h"

void task_1wire(void)
{
	while(1)
	{
	}
}




#define wait(a) delay_us(a)

//****** V A R I A B L E S ********************************************************/
unsigned char macro_delay;

/**********************************************************************
* Function:        void drive_OW_low (void)
* PreCondition:    None
* Input:		   None	
* Output:		   None	
* Overview:		   Configure the OW_PIN as Output and drive the OW_PIN LOW.	
***********************************************************************/
void drive_OW_low (void)
{
	OW_PIN_DIRECTION = OUTPUT;
	OW_WRITE_PIN=LOW;
}

/**********************************************************************
* Function:        void drive_OW_high (void)
* PreCondition:    None
* Input:		   None	
* Output:		   None	
* Overview:		   Configure the OW_PIN as Output and drive the OW_PIN HIGH.	
***********************************************************************/
void drive_OW_high (void)
{
	OW_PIN_DIRECTION = OUTPUT;
	OW_WRITE_PIN = HIGH;	
}

/**********************************************************************
* Function:        unsigned char read_OW (void)
* PreCondition:    None
* Input:		   None	
* Output:		   Return the status of OW pin.	
* Overview:		   Configure as Input pin and Read the status of OW_PIN 	
***********************************************************************/
unsigned char read_OW (void)
{
	unsigned char read_data=0;
	
	OW_WRITE_PIN = INPUT;

	 if (HIGH == OW_READ_PIN)
	 	read_data = SET;
	 else 	
		read_data = CLEAR;
		
	return read_data;
}

/**********************************************************************
* Function:        unsigned char OW_reset_pulse(void)
* PreCondition:    None
* Input:		   None	
* Output:		   Return the Presense Pulse from the slave.	
* Overview:		   Initialization sequence start with reset pulse.
*				   This code generates reset sequence as per the protocol
***********************************************************************/
unsigned char OW_reset_pulse(void)

{
	unsigned char presence_detect;
	
  	drive_OW_low(); 				// Drive the bus low
 	
 	wait(DELAY_240Us);	  			// delay 480 microsecond (us)
	wait(DELAY_240Us);		

 	drive_OW_high ();  				// Release the bus
	
	wait(DELAY_70Us);				// delay 70 microsecond (us)
	
	presence_detect = read_OW();	//Sample for presence pulse from slave

 	wait(DELAY_205Us);	  			// delay 410 microsecond (us)
	wait(DELAY_205Us);		
	
	drive_OW_high ();		    	// Release the bus
	
	return presence_detect;
}	

/**********************************************************************
* Function:        void OW_write_bit (unsigned char write_data)
* PreCondition:    None
* Input:		   Write a bit to 1-wire slave device.
* Output:		   None
* Overview:		   This function used to transmit a single bit to slave device.
*				   
***********************************************************************/

void OW_write_bit (unsigned char write_bit)
{
	if (write_bit)
	{
		//writing a bit '1'
		drive_OW_low(); 				// Drive the bus low
		wait(DELAY_6Us);				// delay 6 microsecond (us)
		drive_OW_high ();  				// Release the bus
		wait(DELAY_64Us);				// delay 64 microsecond (us)
	}
	else
	{
		//writing a bit '0'
		drive_OW_low(); 				// Drive the bus low
		wait(DELAY_60Us);				// delay 60 microsecond (us)
		drive_OW_high ();  				// Release the bus
		wait(DELAY_10Us);				// delay 10 microsecond for recovery (us)
	}
}	


/**********************************************************************
* Function:        unsigned char OW_read_bit (void)
* PreCondition:    None
* Input:		   None
* Output:		   Return the status of the OW PIN
* Overview:		   This function used to read a single bit from the slave device.
*				   
***********************************************************************/

unsigned char OW_read_bit (void)
{
	unsigned char read_data; 
	//reading a bit 
	drive_OW_low(); 						// Drive the bus low
	wait(DELAY_6Us);						// delay 6 microsecond (us)
	drive_OW_high ();  						// Release the bus
	wait(DELAY_9Us);						// delay 9 microsecond (us)

	read_data = read_OW();					//Read the status of OW_PIN

	wait(DELAY_55Us);						// delay 55 microsecond (us)	
	return read_data;
}

/**********************************************************************
* Function:        void OW_write_byte (unsigned char write_data)
* PreCondition:    None
* Input:		   Send byte to 1-wire slave device
* Output:		   None
* Overview:		   This function used to transmit a complete byte to slave device.
*				   
***********************************************************************/
void OW_write_byte (unsigned char write_data)
{
	unsigned char loop;
	
	for (loop = 0; loop < 8; loop++)
	{
		OW_write_bit(write_data & 0x01); 	//Sending LS-bit first
		write_data >>= 1;					// shift the data byte for the next bit to send
	}	
}	

/**********************************************************************
* Function:        unsigned char OW_read_byte (void)
* PreCondition:    None
* Input:		   None
* Output:		   Return the read byte from slave device
* Overview:		   This function used to read a complete byte from the slave device.
*				   
***********************************************************************/

unsigned char OW_read_byte (void)
{
	unsigned char loop, result=0;
	
	for (loop = 0; loop < 8; loop++)
	{
		
		result >>= 1; 				// shift the result to get it ready for the next bit to receive
		if (OW_read_bit())
		result |= 0x80;				// if result is one, then set MS-bit
	}
	return result;					
}	

/********************************************************************************************
                  E N D     O F     1 W I R E . C  
*********************************************************************************************/

//-----------------------------------------------------------------------------
// Obtain a celsius temperature value from a DS18S20 device.
//
float Read_Temperature_DS18S20(void)
{

	// DS18S20 temperature register format :
	// LS Byte:	b7		b6		b5		b4		b3		b2		b1		b0
	//        	2^6		2^5		2^4		2^3		2^2		2^1		2^0		2^-1
	//
	// MS Byte:	b15		b14		b13		b12		b11		b10		b9		b8
	//        	S		S		S		S		S		S		S		S

	unsigned char get[10];
	int k, temp;
	float ftemp;

	OW_reset_pulse();
	OW_write_byte(SKIP_ROM);						// skip ROM
	OW_write_byte(READ_SCRATCHPAD);						// read scratch pad

	for (k=0; k<9; k++)						// read 9 bytes from scratchpad
		get[k]=OW_read_byte();

	temp = get[0];							// load all 8 bits of the LSBYTE

	if (get[1] > 0x80)						// sign bit set, temp is negative
		temp = temp * -1;

	ftemp = temp;
    return (ftemp/2);
}


//-----------------------------------------------------------------------------
// Obtain a celsius temperature value from a DS18B20 device.
//
float Read_Temperature_DS18B20(void)
{
	// DS18B20 / DS1822 temperature register format :
	// LS Byte:	b7		b6		b5		b4		b3		b2		b1		b0
	//        	2^3		2^2		2^1		2^0		2^-1	2^-2	2^-3	2^-4
	//
	// MS Byte:	b15		b14		b13		b12		b11		b10		b9		b8
	//        	S		S		S		S		S		2^6		2^5		2^4

	unsigned char get[10];
	int k;
	long temp;
	float ftemp;
	
	OW_reset_pulse();
	
	OW_write_byte(SKIP_ROM);						// skip ROM
	OW_write_byte(READ_SCRATCHPAD);						// read scratch pad

	for (k=0; k<9; k++)						// read 9 bytes from scratchpad
		get[k]=OW_read_byte();

	temp = get[1] & 0x7;					// load 3 bits of the MSBYTE, masking any sign bits
	temp = temp << 8;						// shift the MSBYTE left 8 bits
	temp = (temp | get[0]);					// load all 8 bits of the LSBYTE

	if (get[1] > 0x80)						// sign bit set, temp is negative
		temp = temp * -1;

	ftemp = temp;
    return (ftemp/16);
}
